# 公共的工具
import functools

from flask import current_app
from flask import g
from flask import session



def do_index_class(index):
    if index == 0:
        return "first"
    if index == 1:
        return "second"
    if index == 2:
        return "third"

    return ""




# 查询用户的信息的装饰器
def user_data_info(f):
    # 保证被装饰的视图函数的函数名__name__不变
    @functools.wraps(f)
    def wapper(*args,**kwargs):
        from info.models import User
        user_id = session.get("user_id")
        # 如果查询失败--user就没有数据,那么就程序将无法进行下去--无法传递数据到前端--所以给user附个值
        user = None
        # 查询用户是否登陆
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args,**kwargs)
    return wapper